package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SysPermission;
import com.easy.common.bean.SysRole;
import com.easy.common.bean.SysUser;
import com.easy.common.enumbean.UserStatus;
import com.easy.common.service.IUserService;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@DubboService
public class UserService extends ServiceImpl<UserMapper, SysUser> implements IUserService {
    @Autowired
    private RoleService roleService;
    @Override
    public SysUser insert(SysUser user) {
        super.save(user);
        int id=user.getId();
        SysUser result=super.getById(id);
        return result;
    }

    @Override
    public SysUser update(SysUser user) {
        super.updateById(user);
        int id=user.getId();
        SysUser result=super.getById(id);
        return result;
    }

    @Override
    public int delete(Integer id) {
        super.removeById(id);
        return 1;
    }

    @Override
    public SysUser getByID(Integer id) {
        return super.getById(id);
    }

    @Override
    public IPage<SysUser> getListByPage(IPage page, SysUser user) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>(user);
        return super.page(page,wrapper);
    }

    @Override
    public List<SysUser> getList(SysUser user) {
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>(user);
        return super.list(wrapper);
    }

    @Override
    public int setStatus(Integer id) {
        SysUser user=super.getById(id);
        UserStatus status =user.getStatus();
        if(status==UserStatus.DISABLE){
            user.setStatus(UserStatus.NORMAL);
        }else{
            user.setStatus(UserStatus.DISABLE);
        }
        super.updateById(user);
        return 1;
    }

    @Override
    public SysUser assignRole(SysUser user) {
        int userid=user.getId();
        //先清空该用户原有的角色信息
        super.getBaseMapper().deleteUserRoleByUserID(userid);
        //再添加现在需要保存的角色信息
        List< SysRole> roles=user.getRoles();
        for(SysRole role:roles){
            super.getBaseMapper().insertUserRole(userid,role.getId());
        }
        return super.getById(userid);
    }

    @Override
    public SysUser getByUsername(String username) {
        SysUser user=super.getBaseMapper().getOneByUsername(username);
        user.setRoles(getUserRoles(user));
        return user;
    }

    @Override
    public List<SysRole> getUserRoles(SysUser user) {
        List<SysRole> roles=this.getBaseMapper().getUserRoles(user.getId());
        for(SysRole role:roles){
           role.setPermissions(roleService.getRolePermissions(role));
        }
        return roles;
    }


}
